/**
 * Author: ling
 * Create Time: 2018-12-03 13:46
 * Description: 代理模式
 */
// 无代理
Flower = function() {};
xiaoming = {
  sendFlower: function (target) {
    let flower = new Flower();
    target.receiveFlower(flower)
  }
};
A = {
  receiveFlower(flower) {
    console.log('收到花：' + flower)
  },
  listenGoodMood(fn) {
    setTimeout(function () {
      fn()
    }, 1000)
  }
};
xiaoming.sendFlower(A);
// 引入代理后
B = {
  receiveFlower(flower) {
    A.listenGoodMood(function () {
      A.receiveFlower(flower)
    })
  }
};
xiaoming.sendFlower(B);

/**
 * 保护代理：作为中间过滤
 * 虚拟代理：减少开销
 */

// 虚拟代理
B = {
  receiveFlower() {
    A.listenGoodMood(function () {
      let flower = new Flower();
      A.receiveFlower(flower)
    })
  }
};

// 虚拟代理实现图片预加载

/**
 * 通过代理模式，
 * 符合开放封闭原则，也符合单一职责原则
 * 没有增加或改变myImage的接口，而是通过代理对象给系统添加了新的行为
 * 如果将来不再使用预加载，直接请求本体，而不是请求代理对象即可
 */
myImage = (function () {
  let imgNode = document.createElement('img');
  document.body.appendChild(imgNode);

  return {
    setSrc: function (src) {
      imgNode.src = src
    }
  };
  return function (src) {
    imgNode.src = src
  }
})();
proxyImage = (function () {
  let img = new Image();
  img.onload = function () {
    myImage.setSrc(this.src)
  };

  return {
    setSrc: function (src) {
      myImage.setSrc('loading img path');
      img.src = src;
    }
  };
  return function (src) {
    myImage.setSrc('loading img path');
    img.src = src
  }
})();
proxyImage.setSrc('');
proxyImage('');

// 虚拟代理合并http请求
synchronousFile = function (id) {
  console.log('正在同步：' + id)
};

proxySynchronousFile = function () {
  let cache = [],
    timer;

  return function (id) {
    cache.push(id);

    if(timer) {
      return false;
    }

    timer = setTimeout(function () {
      synchronousFile(cache.join(','));

      clearTimeout(timer);
      timer = null;
      cache.length = 0;
    }, 2000)
  }
};

//虚拟代理在惰性应用中的运用

miniConsole = (function () {
  let cache = [];
  let handler = function (ev) {
    if(ev.keyCode === 13) {
      let script = document.createElement('script');

      script.onload = function () {
        for (let fn of cache) {
          fn()
        }
      };

      script.src = 'miniConsole.min.js'
      document.getElementsByTagName('head')[0].appendChild(script);
      document.body.removeEventListener('keydown', handler)
    }
  };

  document.addEventListener('keydown', handler);

  return {
    log: function () {
      let args = arguments;
      cache.push(function () {
        return miniConsole.log.apply(miniConsole, args)
      })
    }
  }

})();

// 缓存代理
mult = function () {
  let a = 1;
  for (let val of arguments) {
    a *= val
  }

  return a;
};

proxyMult = function () {
  let cache = {};

  return function () {
    let args = Array.prototype.join.apply(arguments, ',');
    if(args in cache) {
      return cache[args]
    }

    return cache[args] = mult.apply(this, arguments)
  }
};

// 创建代理工厂
createProxyFactory = function (fn) {
  let cache = {};

  return function () {
    let args = Array.prototype.join.apply(arguments, ',');
    if(args in cache) {
      return cache[args]
    }

    return cache[args] = fn.apply(this, arguments)
  }
};

proxyMult = createProxyFactory(mult);
